var AirAvg = echarts.init(document.getElementById('AirAvg'), 'shine');
var WaterAvg = echarts.init(document.getElementById('WaterAvg'), 'shine');
var count = 0;
window.onload = function() {
	var timer = setInterval(function() {
		$("#time").html(utils.GetDateTime("yyyy-MM-dd HH:mm:ss"));
		if (count == 0 || count % 60 == 0) {
			UIRefresh();
		}
		count++;
	}, 1000);
}

function UIRefresh() {
	api.GetSmartEV(function(result) {
		if (result.Code == 0) {
			var data = JSON.parse(result.Data);
			InitGauging('WaterGauging',data.Water);
			InitGauging('AirGauging',data.Vocs);
			InitAvgChart('AirAvgTitle',AirAvg,data.Series.VocsSeries);
			InitAvgChart('WaterAvgTitle',WaterAvg,data.Series.WaterSeries);
			InitGauging('Alarm',data.Alarm);
			InitGauging('Ep',data.EPData);
		}
	})
}
function InitGauging(id,table) {
	var html = "<table><tbody>";
	for (var i = 0; i < table.length; i++) {
		var item = table[i];
		html += "<tr>"
		for (var j = 0; j < item.length; j++) {
			if (!isNaN(item[j])) {
				html += "<td><b>" + item[j] + "</b></td>";
			}else if (item[j].indexOf('↑')>=0||item[j].indexOf('↓')>=0) {
				html += "<td><a>" + item[j] + "</a></td>";
			}else if(item[j].indexOf(']')>0){
				html += "<td><p>" + item[j] + "</p></td>";
			}
			else {
				html += "<td><span>" + item[j] + "</span></td>";
			}
		}
		html + "</tr>";
	}
	html += "</tbody></table>"
	$('#'+id).html(html);
}
function InitAvgChart(tid,chart,data) {
	var rindex = 0;
	var tick = 0;
	var timer = setInterval(function() {
		if(tick==0 || tick % 100 == 0){
			if(rindex>=data.length){
				rindex = 0;
			}
			$("#"+tid).html(data[rindex].Group+"日均曲线"+"<span>"+tid+"</span>");
			SetChartData(chart,data[rindex].Series)
			rindex ++;
		}
		tick++;
	}, 100);
}
function SetChartData(chart, series) {
	chart.clear();
	chart.setOption(option.getEVBar({
		title: {
			text: '单位：条'
		},
		legend: {
			data: series.zName
		},
		xAxis: {
			data: series.xAxis
		},
		series: series.yAxis
	}));
}
window.onresize = function() {
	WaterAvg.resize();
	AirAvg.resize();
}